package com.cskaoyan.controller.wx_comment;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MemoryBean;
import com.cskaoyan.bean.bo.wx_comment.WxCommentListBO;
import com.cskaoyan.bean.bo.wx_comment.WxCommentPostBO;
import com.cskaoyan.bean.vo.wx_comment.WXCommentPostVO;
import com.cskaoyan.bean.vo.wx_comment.WxCommentListVO;
import com.cskaoyan.bean.vo.wx_comment.WxCountCommentVO;
import com.cskaoyan.service.wx_comment.WxCommentService;
import com.cskaoyan.util.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/21 21:28
 */
@RestController
@RequestMapping("wx/comment")
public class WxCommentController {

    @Autowired
    WxCommentService wxCommentService;

    @Autowired
    MemoryBean memoryBean;

    @GetMapping("list")
    public BaseRespVo printComment(@RequestHeader(Constant.WX_TOKEN_HEADER) String token, WxCommentListBO wxCommentListBO) {
        Integer userId = getUserId(token);
        WxCommentListVO wxCommentListVO = wxCommentService.printCommentList(userId, wxCommentListBO);
        if (wxCommentListVO.getLimit() == null) {
            return BaseRespVo.error(504, "系统错误,请重新登录");
        }
        return BaseRespVo.ok(wxCommentListVO);
    }

    @GetMapping("count")
    public BaseRespVo countComment(Integer valueId, Integer type) {
        WxCountCommentVO wxCountCommentVO = wxCommentService.countComment(valueId, type);
        return BaseRespVo.ok(wxCountCommentVO);
    }

    @PostMapping("post")
    public BaseRespVo postComment(@RequestHeader(Constant.WX_TOKEN_HEADER) String token, @RequestBody WxCommentPostBO wxCommentPostBO) {
        Integer userId = getUserId(token);
        WXCommentPostVO wxCommentPostVO = wxCommentService.postComment(userId, wxCommentPostBO);

        if (wxCommentPostVO.getId() == null) {
            return BaseRespVo.error(504, "系统错误,请重新登录");
        }
        return BaseRespVo.ok(wxCommentPostVO);
    }

    private Integer getUserId(String token) {
        Integer userId = memoryBean.tokenAndUserId.get(token);
        if (userId == null) {
            return -1;
        }
        return userId;
    }

}
